using UnityEngine;
using Framework;
public class NpcAct : NpcAction
{
    private Animator _ani;
    private string _actionName;
    private bool _hasPlay;
    private bool _execute;
    protected override void OnEnter()
    {
        base.OnEnter();
        _actionName = "Attack01";
        _npc.PlayAnimation(_actionName);
        _ani = _npc.GetAnimator();
        _hasPlay = false;
        _execute = false;
        if (_npc.InteractiveTarget != null)
        {
            var display = _npc.GetDisplay();
            display.transform.rotation = Quaternion.LookRotation(_npc.InteractiveTarget.GetWorldPosition() - _npc.GetWorldPosition());
            var type = _npc.InteractiveTarget.GetActorType();
            if (type == ActorType.Door)
            {
                execute();
                this.Finished = true;
            }
            else
            {

            }
        }
    }
    protected override void OnUpdate(float deltaTime, float progress)
    {
        base.OnUpdate(deltaTime, progress);
        var ntime = Utils.Unity.GetAnimatorNormalizedTime(_ani, _actionName);
        if (!_hasPlay)
        {
            if (ntime > 0f && ntime < 1f)
            {
                _hasPlay = true;
            }
        }
        if (_hasPlay)
        {
            if (!_execute)
            {
                if (ntime > 0.4f)
                {
                    _execute = true;
                    execute();
                }
            }
            if (ntime >= 1f)
            {
                this.Finished = true;
            }
        }
    }
    private void execute()
    {
        GameEntry.Context.World.ExecuteInteractive(this._npc, this._npc.InteractiveTarget);
    }
}